package com.kuang.linkedlist;

/*给定一个已排序的链表的头 head,删除所有重复的元素，使每个元素只出现一次 。返回已排序的链表 。

        示例：
        输入：head = [1,1,2]
        输出：[1,2]*/


/**
 * @author kjx
 */
public class DeleteDuplicates {


    //第一种传统的双指针不多废话
    public ListNode deleteDuplicates(ListNode head) {
        //判空
        if (head == null || head.next == null) return head;
        //head和hail双指针判断，result保存一开始的head作为返回的结果
        ListNode hail = head.next;
        ListNode result = head;
        while (hail != null) {
            if (hail.val == head.val) {
                head.next = hail.next;
                hail = hail.next;
            } else {
                head = hail;
                hail = hail.next;
            }
        }
        return result;
    }


    //我们来看看递归
    public ListNode deleteDuplicates2(ListNode head) {
        if (head == null || head.next == null) return head;
        head.next = deleteDuplicates2(head.next);
        return head.val == head.next.val ? head.next : head;
    }
}
